// 运行数据库迁移的脚本
const fs = require('fs');
const path = require('path');
const db = require('./db/db');

// 迁移文件列表
const migrations = [
  'db/update_token_field.sql',
  'db/update_zip_field.sql'
];

async function runMigrations() {
  try {
    console.log('开始执行数据库迁移...');
    
    for (const migrationFile of migrations) {
      console.log(`执行迁移: ${migrationFile}`);
      
      // 读取迁移文件内容
      const migrationSQL = fs.readFileSync(path.join(__dirname, migrationFile), 'utf8');
      
      // 分割成多个SQL语句并执行
      const statements = migrationSQL
        .split(';')
        .map(statement => statement.trim())
        .filter(statement => statement && !statement.startsWith('--'));
      
      for (const statement of statements) {
        if (statement) {
          console.log(`执行SQL: ${statement}`);
          await db.query(statement);
        }
      }
      
      console.log(`迁移完成: ${migrationFile}`);
    }
    
    console.log('所有迁移已完成');
    process.exit(0);
  } catch (error) {
    console.error('迁移过程中发生错误:', error);
    process.exit(1);
  }
}

// 执行迁移
runMigrations(); 